<?php
class UsersController extends AppController {
	var $name         = 'Users';

    function beforeFilter() {
        $this->Auth->fields = array(
            'username' => 'username',
            'password' => 'password'
        );
        $this->Auth->authenticate = ClassRegistry::init('User');
        parent::beforeFilter();
    }

/*
    function index() {
        $this->Auth->allow();
    }
*/
    function add() {
        $this->__setAdditionals();
        parent::add();
    }

/**
 * Login here
 */
    function login() {
        if ( $this->Auth->user('id') ) {
            $this->redirect('/');
        }
        $this->layout = 'login';
    }

/**
 * Logout here
 */
    function logout() {
        $this->layout = 'login';
        $this->redirect($this->Auth->logout());
    }

    function __setAdditionals($update = false) {
         // set provinces option
        $provinces = $this->User->Province->find('all', array(
            'fields' => array('Province.id', 'Province.name'),
            'order' => 'name', 'recursive' => 0
        ));
        $this->set(compact('provinces'));

        if ( $update ) {
            $cities = $this->User->City->find('all', array(
                'conditions' => array('City.province_id' => $this->data['User']['province_id']),
                'fields' => array('City.id', 'City.name'),
                'order' => 'name', 'recursive' => 0
            ));
            $this->set('cities', $cities);
        }
        
        // set ajax URL
        $prefix = parent::__pathToController() . '/getOptions';
        $this->set('ajaxURL', "var ajaxURL = '" . $prefix . "';");
    }
}
?>
